#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# ======================================================
# @File:      : app
# @Author     : forward_huan
# @Date       : 2025/1/5 16:37
# @Desc       :
# ======================================================
import os.path

from flask import Flask

from common.logger import addConsoleHandler, addFileHandler, logger
from sqlite_tool import SqliteInit, execute_sql
from upload_file.model.file_model import FileModel
from upload_file.utils.path_utils import get_static_path, get_template_path, get_log_path, get_db_path
from upload_file.utils.utils import get_flask_conf, get_address_conf
from upload_file.view.base_bp import base_bp

app = Flask(__name__, static_folder=get_static_path(), template_folder=get_template_path())


def register_app():
    app.config.from_mapping(get_flask_conf())
    app.register_blueprint(base_bp)


def create_database():
    db_path = get_db_path()
    if os.path.exists(db_path):
        return
    execute_sql(FileModel.create_sql_str())


def start_app():
    SqliteInit.init(get_db_path())
    create_database()
    register_app()
    addConsoleHandler(app.logger)
    addFileHandler(logger, get_log_path())
    app.run(**get_address_conf())
